#include <iostream>
#include <set>
#include <map>
#include <string>
#include <utility>
using std::cout;
using std::endl;
using std::set;
using std::map;
using std::pair;
using std::string;
using std::make_pair;

void test0(){
    pair<int,string> pa1(1,"hello");
    pair<int,string> pa2(2,"world");
    pair<int,string> pa3(3,"wangdao");

    map<int,string> nums = {
        pa1,pa2,pa3,
        {4,"wuhan"},
        {6,"beijing"},
        pair<int,string>(7,"shanghai"),
        pair<int,string>(9,"shenzhen"),
        make_pair(9,"guangzhou"),
        make_pair(3,"hangzhou"),
        make_pair(10,"nanjing"),
        {12,"world"},
        {0,"chengdu"}
    };

    for(auto & ele : nums){
        cout << ele.first << " " << ele.second << endl;
    }

    cout << endl;
    for(auto it = nums.begin(); it != nums.end(); ++it)
    {
        /* cout << (*it).first << " " << (*it).second << endl; */
        cout << it->first << " " << it->second << endl;
    }
}

void test1(){
    map<int,string> nums = {
        {1,"yichun"},
        {2,"nanchang"},
        {3,"LA"},
        {6,"shangluo"},
        {2,"beijing"}
    };

    //map的查找操作
    size_t cnt = nums.count(20);
    cout << cnt << endl;

    /* auto it = nums.find(3); */
    map<int,string>::iterator it = nums.find(3);
    if(it != nums.end()){
        cout << "该元素存在" << endl;
        cout << it->first << " " << it->second << endl;
    }else{
        cout << "该元素不存在" << endl;
    }
}

void test2(){
    map<int,string> nums = {
        {1,"C++"},
        {3,"++C"},
        {7,"xixi"},
        {5,"ganyu"},
        {0,"huasheng"}
    };
    pair<map<int,string>::iterator,bool> ret = 
    nums.insert(pair<int,string>(0,"haibao"));

    if(ret.second){
        cout << "插入成功" << endl;
        cout << ret.first->first << " "  
            << ret.first->second << endl;
    }else{
        cout << "插入失败" << endl;
        cout << ret.first->first << " "  
            << ret.first->second << endl;
    }

    cout << endl;
    cout << "插入多个元素" << endl;
    map<int,string> nums2 = {{1,"beijing"},{18,"shanghai"}};
    nums.insert(nums2.begin(),nums2.end());
    
    nums.insert({{1,"hello"},{2,"world"}});
}

void test3(){
     map<int,string> nums = {
        {1,"C++"},
        {3,"++C"},
        {7,"xixi"},
        {5,"ganyu"},
        {0,"huasheng"}
        };

     cout << nums[0] << endl;
     cout << nums[1] << endl;
     cout << nums[2] << endl;
     cout << nums[3] << endl;

    cout << endl;
    for(auto & ele : nums){
        cout << ele.first << " " << ele.second << endl;
    }

    nums[2] = "hello";
    cout << endl;
    for(auto & ele : nums){
        cout << ele.first << " " << ele.second << endl;
    }
}

void test4(){
    map<string,int> nums = {{"hello",1},{"world",2}};
    cout << nums["hello"] << endl;
    cout << nums["world"] << endl;
    cout << nums["wangdao"] << endl;
    cout << endl;
    for(auto & ele : nums){
        cout << ele.first << " " << ele.second << endl;
    }
}

int main(void){
    test4();
    return 0;
}
